home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 15 / Example 15.1 / mouse.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-17  |  3.3 KB  |  138 lines

  1. #include "mouse.h"
  2.  
  3. //////////////////////////////////////////////////////////////////////////
  4. //                        MOUSE                                            //
  5. //////////////////////////////////////////////////////////////////////////
  6.  
  7. MOUSE::MOUSE()
  8. {
  9.     m_iType = 0;
  10.     m_pMouseTexture = NULL;
  11.     m_pMouseDevice = NULL;
  12.     m_fSpeed = 1.5f;
  13.     m_dwBlock = GetTickCount();
  14. }
  15.  
  16. MOUSE::~MOUSE()
  17. {
  18.     if(m_pMouseDevice != NULL)
  19.         m_pMouseDevice->Release();
  20.  
  21.     if(m_pMouseTexture != NULL)
  22.         m_pMouseTexture->Release();
  23. }
  24.  
  25. void MOUSE::InitMouse(IDirect3DDevice9* Dev, HWND wnd)
  26. {
  27.     try
  28.     {
  29.         m_pDevice = Dev;
  30.  
  31.         //Load texture and init sprite
  32.         D3DXCreateTextureFromFile(m_pDevice, "cursor/cursor.dds", &m_pMouseTexture);
  33.         D3DXCreateSprite(m_pDevice, &m_pSprite);
  34.  
  35.         //Get directinput object
  36.         LPDIRECTINPUT8 directInput = NULL;
  37.  
  38.         DirectInput8Create(GetModuleHandle(NULL),    // Retrieves the application handle
  39.                            0x0800,                    // v.8.0    
  40.                            IID_IDirectInput8,        // Interface ID
  41.                            (VOID**)&directInput,    // Our DirectInput Object
  42.                            NULL);
  43.  
  44.         //Acquire Default System mouse
  45.         directInput->CreateDevice(GUID_SysMouse, &m_pMouseDevice, NULL);        
  46.         m_pMouseDevice->SetDataFormat(&c_dfDIMouse);
  47.         m_pMouseDevice->SetCooperativeLevel(wnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
  48.         m_pMouseDevice->Acquire();            
  49.  
  50.         //Get viewport size and init mouse location
  51.         D3DVIEWPORT9 v;
  52.         m_pDevice->GetViewport(&v);
  53.         m_viewport.left = v.X;
  54.         m_viewport.top = v.Y;
  55.         m_viewport.right = v.X + v.Width;
  56.         m_viewport.bottom = v.Y + v.Height;
  57.  
  58.         x = v.X + v.Width / 2.0f;
  59.         y = v.Y + v.Height / 2.0f;
  60.  
  61.         //Release Direct Input Object
  62.         directInput->Release();
  63.     }
  64.     catch(...)
  65.     {
  66.         debug.Print("Error in MOUSE::InitMouse()");
  67.     }    
  68. }
  69.  
  70. bool MOUSE::ClickLeft()
  71.     if(GetTickCount() < m_dwBlock)return false;
  72.     return m_mouseState.rgbButtons[0];
  73. }
  74.  
  75. bool MOUSE::ClickRight()
  76. {
  77.     if(GetTickCount() < m_dwBlock)return false;
  78.     return m_mouseState.rgbButtons[1];
  79. }
  80.  
  81. bool MOUSE::WheelUp()
  82. {
  83.     if(GetTickCount() < m_dwBlock)return false;
  84.     return m_mouseState.lZ > 0.0f;
  85. }
  86.  
  87. bool MOUSE::WheelDown()
  88. {
  89.     if(GetTickCount() < m_dwBlock)return false;
  90.     return m_mouseState.lZ < 0.0f;
  91. }
  92.  
  93. bool MOUSE::Over(RECT dest)
  94. {
  95.     if(x < dest.left || x > dest.right)return false;
  96.     if(y < dest.top || y > dest.bottom)return false;
  97.     return true;
  98. }
  99.  
  100. bool MOUSE::PressInRect(RECT dest)
  101. {
  102.     return ClickLeft() && Over(dest);
  103. }
  104.  
  105. void MOUSE::Update()
  106. {
  107.     //Retrieve mouse state
  108.     ZeroMemory(&m_mouseState, sizeof(DIMOUSESTATE));
  109.     m_pMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE), &m_mouseState);
  110.  
  111.     //Update pointer
  112.     x += m_mouseState.lX * m_fSpeed;
  113.     y += m_mouseState.lY * m_fSpeed;
  114.  
  115.     //Keep mouse pointer within window
  116.     if(x < m_viewport.left)    x = m_viewport.left;
  117.     if(y < m_viewport.top)    y = m_viewport.top;
  118.     if(x > m_viewport.right)    x = m_viewport.right;
  119.     if(y > m_viewport.bottom)    y = m_viewport.bottom;
  120. }
  121.  
  122. void MOUSE::Paint()
  123. {    
  124.     if(m_pMouseTexture != NULL)
  125.     {
  126.         RECT src[] = {{0,0,20,20}, {0,20,20,40}, {20,20,40,40}, {0,40,20,60}, {20,40,40,60}};
  127.  
  128.         m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
  129.         m_pSprite->Draw(m_pMouseTexture, &src[m_iType], NULL, &D3DXVECTOR3(x, y, 0.0f),    0xffffffff);
  130.         m_pSprite->End();
  131.     }    
  132. }
  133.  
  134. void MOUSE::DisableInput(int millisec)
  135. {
  136.     m_dwBlock = GetTickCount() + millisec;
  137. }